package com.ag.work2.action;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.regex.Pattern;

import javax.annotation.Resource;

import org.apache.struts2.interceptor.SessionAware;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.ag.bean.TbEvent;
import com.ag.bean.TbPlanStandard;
import com.ag.bean.TbPowerTransfer;
import com.ag.bean.TbUser;
import com.ag.common.AjaxNotify;
import com.ag.common.BaseAction;
import com.ag.common.EventUtil;
import com.ag.common.IPermissionService;
import com.ag.work2.service.IPlanStartStandardService;

@Scope("prototype")
@Controller("LZMPlanStartStandardAction")
public class PlanStartStandardAction extends BaseAction implements SessionAware {
	private static final long serialVersionUID = 7102838107305177388L;
	/**
	 * 调用权限服务逻辑组件
	 */
	@Resource(name = "PermissionServiceImpl")
	private IPermissionService pms;
	/**
	 * 查询所有记录数的返回载体
	 */
	private Long recordCount;
	private int firstRow;
	private String elidsString;

	/*
	 * 标准等级
	 */
	private int planLevel;

	public void setPlanLevel(int planLevel) {
		this.planLevel = planLevel;
	}

	public String getElidsString() {
		return elidsString;
	}

	public void setElidsString(String elidsString) {
		this.elidsString = elidsString;
	}

	public void setFirstRow(int firstRow) {
		this.firstRow = firstRow;
	}

	private int maxRow;

	public void setMaxRow(int maxRow) {
		this.maxRow = maxRow;
	}

	public Long getRecordCount() {
		return recordCount;
	}

	/**
	 * 提供Map<String, Object>类型的Session变量，用来保存Struts2注入的Session上下文
	 */
	private String elId;

	private List<TbPowerTransfer> powerTransfers;

	public List<TbPowerTransfer> getPowerTransfers() {
		return powerTransfers;
	}

	private TbEvent event;

	private TbUser dstUser;

	public TbUser getDstUser() {
		return dstUser;
	}

	public void setDstUser(TbUser dstUser) {
		this.dstUser = dstUser;
	}

	public TbEvent getEvent() {
		return event;
	}

	public void setElId(String elId) {
		this.elId = elId;
	}

	private Map<String, Object> sessionMap;
	private String ajaxRst;
	private List<TbPlanStandard> planStandards;

	public List<TbPlanStandard> getPlanStandards() {
		return planStandards;
	}

	public String getAjaxRst() {
		return ajaxRst;
	}

	@Resource(name = "LZMPlanStartStandardServiceImpl")
	private IPlanStartStandardService psss;

	/**
	 * 查找所有启动标准
	 * 
	 * @return
	 */

	public String findPlanStartStandard() {
		try {
			planStandards = psss.findPlanStartStandard(planLevel);
			ajaxRst = AjaxNotify.SUCCESS;
			return "find_plan_start_standard";
		} catch (Exception e) {
			e.printStackTrace();
			ajaxRst = AjaxNotify.EXCEPTION;
			return "operate";
		}
	}

	/**
	 * 查找权责转移 条数
	 * 
	 * @return
	 */

	public String findPowerTransferRC() {
		try {
			recordCount = psss.findPowerTransferRC(pms
					.getCurrentUser(sessionMap));
			ajaxRst = AjaxNotify.SUCCESS;
			return "find_power_transfer_rc";
		} catch (Exception e) {
			e.printStackTrace();
			ajaxRst = AjaxNotify.EXCEPTION;
			return "operate";
		}
	}

	/**
	 * 查找权责转移
	 * 
	 * @return
	 */

	public String findPowerTransfer() {
		try {
			powerTransfers = psss.findPowerTransfer(pms
					.getCurrentUser(sessionMap), firstRow, maxRow);
			ajaxRst = AjaxNotify.SUCCESS;
			return "find_power_transfer";
		} catch (Exception e) {
			e.printStackTrace();
			ajaxRst = AjaxNotify.EXCEPTION;
			return "operate";
		}
	}

	/**
	 * 保存权责转移
	 * 
	 * @return
	 */

	public String savePowerTransfer() {
		try {
			psss.savePowerTransfer(pms.getCurrentUser(sessionMap), dstUser,
					(String) sessionMap.get(EventUtil.PROCESSING_EVENT_ID));
			ajaxRst = AjaxNotify.SUCCESS;
			return "operate";
		} catch (Exception e) {
			e.printStackTrace();
			ajaxRst = AjaxNotify.EXCEPTION;
			return "operate";
		}
	}

	/**
	 * 查找当前事件
	 * 
	 * @return
	 */

	public String findCurrentEvent() {
		try {

			event = psss.findCurrentEvent((String) sessionMap
					.get(EventUtil.PROCESSING_EVENT_ID));
			ajaxRst = AjaxNotify.SUCCESS;
			return "find_event_single";
		} catch (Exception e) {
			e.printStackTrace();
			ajaxRst = AjaxNotify.EXCEPTION;
			return "operate";
		}
	}

	/**
	 * 保存启动标准
	 * 
	 * @return
	 */

	public String savePlanStartStandard() {
		try {
			// String[] elIdArray=Pattern.compile(",").split(elidsString);

			psss.savePlanStartStandard(elidsString, (String) sessionMap
					.get(EventUtil.PROCESSING_EVENT_ID));

			ajaxRst = AjaxNotify.SUCCESS;
			return "operate";
		} catch (Exception e) {
			e.printStackTrace();
			ajaxRst = AjaxNotify.EXCEPTION;
			return "operate";
		}
	}

	@Override
	public void setSession(Map<String, Object> session) {
		this.sessionMap = session;
	}

}
